phpseclib/Crypt/Base.php
- 1
<?php
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67 define('CRYPT_MODE_CTR', -1);
- 68
- 69
- 70
- 71
- 72
- 73 define('CRYPT_MODE_ECB', 1);
- 74
- 75
- 76
- 77
- 78
- 79 define('CRYPT_MODE_CBC', 2);
- 80
- 81
- 82
- 83
- 84
- 85 define('CRYPT_MODE_CFB', 3);
- 86
- 87
- 88
- 89
- 90
- 91 define('CRYPT_MODE_OFB', 4);
- 92
- 93
- 94
- 95 define('CRYPT_MODE_STREAM', 5);
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106 define('CRYPT_ENGINE_INTERNAL', 1);
- 107
- 108
- 109
- 110 define('CRYPT_ENGINE_MCRYPT', 2);
- 111
- 112
- 113
- 114 define('CRYPT_ENGINE_OPENSSL', 3);
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125 class Crypt_Base
- 126 {
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134 var $mode;
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142 var $block_size = 16;
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151 var $key = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160 var $iv;
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170 var $encryptIV;
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180 var $decryptIV;
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189 var $continuousBuffer = false;
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199 var $enbuffer;
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209 var $debuffer;
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221 var $enmcrypt;
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233 var $demcrypt;
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243 var $enchanged = true;
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253 var $dechanged = true;
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272 var $ecb;
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294 var $cfb_init_len = 600;
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305 var $changed = true;
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314 var $padding = true;
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323 var $paddable = false;
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340 var $engine;
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350 var $preferredEngine;
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363 var $cipher_name_mcrypt;
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374 var $cipher_name_openssl;
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386 var $cipher_name_openssl_ecb;
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395 var $password_default_salt = 'phpseclib/salt';
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 403
- 404
- 405
- 406
- 407
- 408
- 409
- 410
- 411
- 412
- 413
- 414
- 415
- 416
- 417
- 418 var $const_namespace;
- 419
- 420
- 421
- 422
- 423
- 424
- 425
- 426
- 427
- 428
- 429
- 430
- 431
- 432
- 433 var $inline_crypt;
- 434
- 435
- 436
- 437
- 438
- 439
- 440
- 441
- 442
- 443
- 444 var $use_inline_crypt;
- 445
- 446
- 447
- 448
- 449
- 450
- 451
- 452
- 453 var $openssl_emulate_ctr = false;
- 454
- 455
- 456
- 457
- 458
- 459
- 460
- 461
- 462 var $openssl_options;
- 463
- 464
- 465
- 466
- 467
- 468
- 469
- 470
- 471 var $explicit_key_length = false;
- 472
- 473
- 474
- 475
- 476
- 477
- 478
- 479
- 480 var $skip_key_adjustment = false;
- 481
- 482
- 483
- 484
- 485
- 486
- 487
- 488
- 489
- 490
- 491
- 492
- 493
- 494
- 495
- 496
- 497
- 498
- 499
- 500
- 501
- 502
- 503
- 504
- 505
- 506 function __construct($mode = CRYPT_MODE_CBC)
- 507 {
- 508
- 509 switch ($mode) {
- 510 case CRYPT_MODE_ECB:
- 511 $this->paddable = true;
- 512 $this->mode = CRYPT_MODE_ECB;
- 513 break;
- 514 case CRYPT_MODE_CTR:
- 515 case CRYPT_MODE_CFB:
- 516 case CRYPT_MODE_OFB:
- 517 case CRYPT_MODE_STREAM:
- 518 $this->mode = $mode;
- 519 break;
- 520 case CRYPT_MODE_CBC:
- 521 default:
- 522 $this->paddable = true;
- 523 $this->mode = CRYPT_MODE_CBC;
- 524 }
- 525
- 526 $this->_setEngine();
- 527
- 528
- 529 if ($this->use_inline_crypt !== false) {
- 530 $this->use_inline_crypt = version_compare(PHP_VERSION, '5.3.0') >= 0 || function_exists('create_function');
- 531 }
- 532 }
- 533
- 534
- 535
- 536
- 537
- 538
- 539
- 540
- 541 function Crypt_Base($mode = CRYPT_MODE_CBC)
- 542 {
- 543 $this->__construct($mode);
- 544 }
- 545
- 546
- 547
- 548
- 549
- 550
- 551
- 552
- 553
- 554
- 555
- 556 function setIV($iv)
- 557 {
- 558 if ($this->mode == CRYPT_MODE_ECB) {
- 559 return;
- 560 }
- 561
- 562 $this->iv = $iv;
- 563 $this->changed = true;
- 564 }
- 565
- 566
- 567
- 568
- 569
- 570
- 571
- 572
- 573
- 574 function setKeyLength($length)
- 575 {
- 576 $this->explicit_key_length = true;
- 577 $this->changed = true;
- 578 $this->_setEngine();
- 579 }
- 580
- 581
- 582
- 583
- 584
- 585
- 586
- 587 function getKeyLength()
- 588 {
- 589 return $this->key_length << 3;
- 590 }
- 591
- 592
- 593
- 594
- 595
- 596
- 597
- 598 function getBlockLength()
- 599 {
- 600 return $this->block_size << 3;
- 601 }
- 602
- 603
- 604
- 605
- 606
- 607
- 608
- 609
- 610
- 611
- 612
- 613
- 614
- 615
- 616
- 617 function setKey($key)
- 618 {
- 619 if (!$this->explicit_key_length) {
- 620 $this->setKeyLength(strlen($key) << 3);
- 621 $this->explicit_key_length = false;
- 622 }
- 623
- 624 $this->key = $key;
- 625 $this->changed = true;
- 626 $this->_setEngine();
- 627 }
- 628
- 629
- 630
- 631
- 632
- 633
- 634
- 635
- 636
- 637
- 638
- 639
- 640
- 641
- 642
- 643
- 644
- 645 function setPassword($password, $method = 'pbkdf2')
- 646 {
- 647 $key = '';
- 648
- 649 switch ($method) {
- 650 default:
- 651 $func_args = func_get_args();
- 652
- 653
- 654 $hash = isset($func_args[2]) ? $func_args[2] : 'sha1';
- 655
- 656
- 657 $salt = isset($func_args[3]) ? $func_args[3] : $this->password_default_salt;
- 658
- 659
- 660
- 661 $count = isset($func_args[4]) ? $func_args[4] : 1000;
- 662
- 663
- 664 if (isset($func_args[5])) {
- 665 $dkLen = $func_args[5];
- 666 } else {
- 667 $dkLen = $method == 'pbkdf1' ? 2 * $this->key_length : $this->key_length;
- 668 }
- 669
- 670 switch (true) {
- 671 case $method == 'pbkdf1':
- 672 if (!class_exists('Crypt_Hash')) {
- 673 include_once 'Crypt/Hash.php';
- 674 }
- 675 $hashObj = new Crypt_Hash();
- 676 $hashObj->setHash($hash);
- 677 if ($dkLen > $hashObj->getLength()) {
- 678 user_error('Derived key too long');
- 679 return false;
- 680 }
- 681 $t = $password . $salt;
- 682 for ($i = 0; $i < $count; ++$i) {
- 683 $t = $hashObj->hash($t);
- 684 }
- 685 $key = substr($t, 0, $dkLen);
- 686
- 687 $this->setKey(substr($key, 0, $dkLen >> 1));
- 688 $this->setIV(substr($key, $dkLen >> 1));
- 689
- 690 return true;
- 691
- 692 case !function_exists('hash_pbkdf2'):
- 693 case !function_exists('hash_algos'):
- 694 case !in_array($hash, hash_algos()):
- 695 if (!class_exists('Crypt_Hash')) {
- 696 include_once 'Crypt/Hash.php';
- 697 }
- 698 $i = 1;
- 699 while (strlen($key) < $dkLen) {
- 700 $hmac = new Crypt_Hash();
- 701 $hmac->setHash($hash);
- 702 $hmac->setKey($password);
- 703 $f = $u = $hmac->hash($salt . pack('N', $i++));
- 704 for ($j = 2; $j <= $count; ++$j) {
- 705 $u = $hmac->hash($u);
- 706 $f^= $u;
- 707 }
- 708 $key.= $f;
- 709 }
- 710 $key = substr($key, 0, $dkLen);
- 711 break;
- 712 default:
- 713 $key = hash_pbkdf2($hash, $password, $salt, $count, $dkLen, true);
- 714 }
- 715 }
- 716
- 717 $this->setKey($key);
- 718
- 719 return true;
- 720 }
- 721
- 722
- 723
- 724
- 725
- 726
- 727
- 728
- 729
- 730
- 731
- 732
- 733
- 734
- 735
- 736
- 737
- 738
- 739
- 740
- 741
- 742 function encrypt($plaintext)
- 743 {
- 744 if ($this->paddable) {
- 745 $plaintext = $this->_pad($plaintext);
- 746 }
- 747
- 748 if ($this->engine === CRYPT_ENGINE_OPENSSL) {
- 749 if ($this->changed) {
- 750 $this->_clearBuffers();
- 751 $this->changed = false;
- 752 }
- 753 switch ($this->mode) {
- 754 case CRYPT_MODE_STREAM:
- 755 return openssl_encrypt($plaintext, $this->cipher_name_openssl, $this->key, $this->openssl_options);
- 756 case CRYPT_MODE_ECB:
- 757 $result = openssl_encrypt($plaintext, $this->cipher_name_openssl, $this->key, $this->openssl_options);
- 758 return !defined('OPENSSL_RAW_DATA') ? substr($result, 0, -$this->block_size) : $result;
- 759 case CRYPT_MODE_CBC:
- 760 $result = openssl_encrypt($plaintext, $this->cipher_name_openssl, $this->key, $this->openssl_options, $this->encryptIV);
- 761 if (!defined('OPENSSL_RAW_DATA')) {
- 762 $result = substr($result, 0, -$this->block_size);
- 763 }
- 764 if ($this->continuousBuffer) {
- 765 $this->encryptIV = substr($result, -$this->block_size);
- 766 }
- 767 return $result;
- 768 case CRYPT_MODE_CTR:
- 769 return $this->_openssl_ctr_process($plaintext, $this->encryptIV, $this->enbuffer);
- 770 case CRYPT_MODE_CFB:
- 771
- 772
- 773 $ciphertext = '';
- 774 if ($this->continuousBuffer) {
- 775 $iv = &$this->encryptIV;
- 776 $pos = &$this->enbuffer['pos'];
- 777 } else {
- 778 $iv = $this->encryptIV;
- 779 $pos = 0;
- 780 }
- 781 $len = strlen($plaintext);
- 782 $i = 0;
- 783 if ($pos) {
- 784 $orig_pos = $pos;
- 785 $max = $this->block_size - $pos;
- 786 if ($len >= $max) {
- 787 $i = $max;
- 788 $len-= $max;
- 789 $pos = 0;
- 790 } else {
- 791 $i = $len;
- 792 $pos+= $len;
- 793 $len = 0;
- 794 }
- 795
- 796 $ciphertext = substr($iv, $orig_pos) ^ $plaintext;
- 797 $iv = substr_replace($iv, $ciphertext, $orig_pos, $i);
- 798 $plaintext = substr($plaintext, $i);
- 799 }
- 800
- 801 $overflow = $len % $this->block_size;
- 802
- 803 if ($overflow) {
- 804 $ciphertext.= openssl_encrypt(substr($plaintext, 0, -$overflow) . str_repeat("\0", $this->block_size), $this->cipher_name_openssl, $this->key, $this->openssl_options, $iv);
- 805 $iv = $this->_string_pop($ciphertext, $this->block_size);
- 806
- 807 $size = $len - $overflow;
- 808 $block = $iv ^ substr($plaintext, -$overflow);
- 809 $iv = substr_replace($iv, $block, 0, $overflow);
- 810 $ciphertext.= $block;
- 811 $pos = $overflow;
- 812 } elseif ($len) {
- 813 $ciphertext = openssl_encrypt($plaintext, $this->cipher_name_openssl, $this->key, $this->openssl_options, $iv);
- 814 $iv = substr($ciphertext, -$this->block_size);
- 815 }
- 816
- 817 return $ciphertext;
- 818 case CRYPT_MODE_OFB:
- 819 return $this->_openssl_ofb_process($plaintext, $this->encryptIV, $this->enbuffer);
- 820 }
- 821 }
- 822
- 823 if ($this->engine === CRYPT_ENGINE_MCRYPT) {
- 824 if ($this->changed) {
- 825 $this->_setupMcrypt();
- 826 $this->changed = false;
- 827 }
- 828 if ($this->enchanged) {
- 829 @mcrypt_generic_init($this->enmcrypt, $this->key, $this->encryptIV);
- 830 $this->enchanged = false;
- 831 }
- 832
- 833
- 834
- 835
- 836 if ($this->mode == CRYPT_MODE_CFB && $this->continuousBuffer) {
- 837 $block_size = $this->block_size;
- 838 $iv = &$this->encryptIV;
- 839 $pos = &$this->enbuffer['pos'];
- 840 $len = strlen($plaintext);
- 841 $ciphertext = '';
- 842 $i = 0;
- 843 if ($pos) {
- 844 $orig_pos = $pos;
- 845 $max = $block_size - $pos;
- 846 if ($len >= $max) {
- 847 $i = $max;
- 848 $len-= $max;
- 849 $pos = 0;
- 850 } else {
- 851 $i = $len;
- 852 $pos+= $len;
- 853 $len = 0;
- 854 }
- 855 $ciphertext = substr($iv, $orig_pos) ^ $plaintext;
- 856 $iv = substr_replace($iv, $ciphertext, $orig_pos, $i);
- 857 $this->enbuffer['enmcrypt_init'] = true;
- 858 }
- 859 if ($len >= $block_size) {
- 860 if ($this->enbuffer['enmcrypt_init'] === false || $len > $this->cfb_init_len) {
- 861 if ($this->enbuffer['enmcrypt_init'] === true) {
- 862 @mcrypt_generic_init($this->enmcrypt, $this->key, $iv);
- 863 $this->enbuffer['enmcrypt_init'] = false;
- 864 }
- 865 $ciphertext.= @mcrypt_generic($this->enmcrypt, substr($plaintext, $i, $len - $len % $block_size));
- 866 $iv = substr($ciphertext, -$block_size);
- 867 $len%= $block_size;
- 868 } else {
- 869 while ($len >= $block_size) {
- 870 $iv = @mcrypt_generic($this->ecb, $iv) ^ substr($plaintext, $i, $block_size);
- 871 $ciphertext.= $iv;
- 872 $len-= $block_size;
- 873 $i+= $block_size;
- 874 }
- 875 }
- 876 }
- 877
- 878 if ($len) {
- 879 $iv = @mcrypt_generic($this->ecb, $iv);
- 880 $block = $iv ^ substr($plaintext, -$len);
- 881 $iv = substr_replace($iv, $block, 0, $len);
- 882 $ciphertext.= $block;
- 883 $pos = $len;
- 884 }
- 885
- 886 return $ciphertext;
- 887 }
- 888
- 889 $ciphertext = @mcrypt_generic($this->enmcrypt, $plaintext);
- 890
- 891 if (!$this->continuousBuffer) {
- 892 @mcrypt_generic_init($this->enmcrypt, $this->key, $this->encryptIV);
- 893 }
- 894
- 895 return $ciphertext;
- 896 }
- 897
- 898 if ($this->changed) {
- 899 $this->_setup();
- 900 $this->changed = false;
- 901 }
- 902 if ($this->use_inline_crypt) {
- 903 $inline = $this->inline_crypt;
- 904 return $inline('encrypt', $this, $plaintext);
- 905 }
- 906
- 907 $buffer = &$this->enbuffer;
- 908 $block_size = $this->block_size;
- 909 $ciphertext = '';
- 910 switch ($this->mode) {
- 911 case CRYPT_MODE_ECB:
- 912 for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
- 913 $ciphertext.= $this->_encryptBlock(substr($plaintext, $i, $block_size));
- 914 }
- 915 break;
- 916 case CRYPT_MODE_CBC:
- 917 $xor = $this->encryptIV;
- 918 for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
- 919 $block = substr($plaintext, $i, $block_size);
- 920 $block = $this->_encryptBlock($block ^ $xor);
- 921 $xor = $block;
- 922 $ciphertext.= $block;
- 923 }
- 924 if ($this->continuousBuffer) {
- 925 $this->encryptIV = $xor;
- 926 }
- 927 break;
- 928 case CRYPT_MODE_CTR:
- 929 $xor = $this->encryptIV;
- 930 if (strlen($buffer['ciphertext'])) {
- 931 for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
- 932 $block = substr($plaintext, $i, $block_size);
- 933 if (strlen($block) > strlen($buffer['ciphertext'])) {
- 934 $buffer['ciphertext'].= $this->_encryptBlock($xor);
- 935 }
- 936 $this->_increment_str($xor);
- 937 $key = $this->_string_shift($buffer['ciphertext'], $block_size);
- 938 $ciphertext.= $block ^ $key;
- 939 }
- 940 } else {
- 941 for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
- 942 $block = substr($plaintext, $i, $block_size);
- 943 $key = $this->_encryptBlock($xor);
- 944 $this->_increment_str($xor);
- 945 $ciphertext.= $block ^ $key;
- 946 }
- 947 }
- 948 if ($this->continuousBuffer) {
- 949 $this->encryptIV = $xor;
- 950 if ($start = strlen($plaintext) % $block_size) {
- 951 $buffer['ciphertext'] = substr($key, $start) . $buffer['ciphertext'];
- 952 }
- 953 }
- 954 break;
- 955 case CRYPT_MODE_CFB:
- 956
- 957
- 958 if ($this->continuousBuffer) {
- 959 $iv = &$this->encryptIV;
- 960 $pos = &$buffer['pos'];
- 961 } else {
- 962 $iv = $this->encryptIV;
- 963 $pos = 0;
- 964 }
- 965 $len = strlen($plaintext);
- 966 $i = 0;
- 967 if ($pos) {
- 968 $orig_pos = $pos;
- 969 $max = $block_size - $pos;
- 970 if ($len >= $max) {
- 971 $i = $max;
- 972 $len-= $max;
- 973 $pos = 0;
- 974 } else {
- 975 $i = $len;
- 976 $pos+= $len;
- 977 $len = 0;
- 978 }
- 979
- 980 $ciphertext = substr($iv, $orig_pos) ^ $plaintext;
- 981 $iv = substr_replace($iv, $ciphertext, $orig_pos, $i);
- 982 }
- 983 while ($len >= $block_size) {
- 984 $iv = $this->_encryptBlock($iv) ^ substr($plaintext, $i, $block_size);
- 985 $ciphertext.= $iv;
- 986 $len-= $block_size;
- 987 $i+= $block_size;
- 988 }
- 989 if ($len) {
- 990 $iv = $this->_encryptBlock($iv);
- 991 $block = $iv ^ substr($plaintext, $i);
- 992 $iv = substr_replace($iv, $block, 0, $len);
- 993 $ciphertext.= $block;
- 994 $pos = $len;
- 995 }
- 996 break;
- 997 case CRYPT_MODE_OFB:
- 998 $xor = $this->encryptIV;
- 999 if (strlen($buffer['xor'])) {
- 1000 for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
- 1001 $block = substr($plaintext, $i, $block_size);
- 1002 if (strlen($block) > strlen($buffer['xor'])) {
- 1003 $xor = $this->_encryptBlock($xor);
- 1004 $buffer['xor'].= $xor;
- 1005 }
- 1006 $key = $this->_string_shift($buffer['xor'], $block_size);
- 1007 $ciphertext.= $block ^ $key;
- 1008 }
- 1009 } else {
- 1010 for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
- 1011 $xor = $this->_encryptBlock($xor);
- 1012 $ciphertext.= substr($plaintext, $i, $block_size) ^ $xor;
- 1013 }
- 1014 $key = $xor;
- 1015 }
- 1016 if ($this->continuousBuffer) {
- 1017 $this->encryptIV = $xor;
- 1018 if ($start = strlen($plaintext) % $block_size) {
- 1019 $buffer['xor'] = substr($key, $start) . $buffer['xor'];
- 1020 }
- 1021 }
- 1022 break;
- 1023 case CRYPT_MODE_STREAM:
- 1024 $ciphertext = $this->_encryptBlock($plaintext);
- 1025 break;
- 1026 }
- 1027
- 1028 return $ciphertext;
- 1029 }
- 1030
- 1031
- 1032
- 1033
- 1034
- 1035
- 1036
- 1037
- 1038
- 1039
- 1040
- 1041
- 1042
- 1043 function decrypt($ciphertext)
- 1044 {
- 1045 if ($this->paddable) {
- 1046
- 1047
- 1048 $ciphertext = str_pad($ciphertext, strlen($ciphertext) + ($this->block_size - strlen($ciphertext) % $this->block_size) % $this->block_size, chr(0));
- 1049 }
- 1050
- 1051 if ($this->engine === CRYPT_ENGINE_OPENSSL) {
- 1052 if ($this->changed) {
- 1053 $this->_clearBuffers();
- 1054 $this->changed = false;
- 1055 }
- 1056 switch ($this->mode) {
- 1057 case CRYPT_MODE_STREAM:
- 1058 $plaintext = openssl_decrypt($ciphertext, $this->cipher_name_openssl, $this->key, $this->openssl_options);
- 1059 break;
- 1060 case CRYPT_MODE_ECB:
- 1061 if (!defined('OPENSSL_RAW_DATA')) {
- 1062 $ciphertext.= openssl_encrypt('', $this->cipher_name_openssl_ecb, $this->key, true);
- 1063 }
- 1064 $plaintext = openssl_decrypt($ciphertext, $this->cipher_name_openssl, $this->key, $this->openssl_options);
- 1065 break;
- 1066 case CRYPT_MODE_CBC:
- 1067 if (!defined('OPENSSL_RAW_DATA')) {
- 1068 $padding = str_repeat(chr($this->block_size), $this->block_size) ^ substr($ciphertext, -$this->block_size);
- 1069 $ciphertext.= substr(openssl_encrypt($padding, $this->cipher_name_openssl_ecb, $this->key, true), 0, $this->block_size);
- 1070 $offset = 2 * $this->block_size;
- 1071 } else {
- 1072 $offset = $this->block_size;
- 1073 }
- 1074 $plaintext = openssl_decrypt($ciphertext, $this->cipher_name_openssl, $this->key, $this->openssl_options, $this->decryptIV);
- 1075 if ($this->continuousBuffer) {
- 1076 $this->decryptIV = substr($ciphertext, -$offset, $this->block_size);
- 1077 }
- 1078 break;
- 1079 case CRYPT_MODE_CTR:
- 1080 $plaintext = $this->_openssl_ctr_process($ciphertext, $this->decryptIV, $this->debuffer);
- 1081 break;
- 1082 case CRYPT_MODE_CFB:
- 1083
- 1084
- 1085 $plaintext = '';
- 1086 if ($this->continuousBuffer) {
- 1087 $iv = &$this->decryptIV;
- 1088 $pos = &$this->buffer['pos'];
- 1089 } else {
- 1090 $iv = $this->decryptIV;
- 1091 $pos = 0;
- 1092 }
- 1093 $len = strlen($ciphertext);
- 1094 $i = 0;
- 1095 if ($pos) {
- 1096 $orig_pos = $pos;
- 1097 $max = $this->block_size - $pos;
- 1098 if ($len >= $max) {
- 1099 $i = $max;
- 1100 $len-= $max;
- 1101 $pos = 0;
- 1102 } else {
- 1103 $i = $len;
- 1104 $pos+= $len;
- 1105 $len = 0;
- 1106 }
- 1107
- 1108 $plaintext = substr($iv, $orig_pos) ^ $ciphertext;
- 1109 $iv = substr_replace($iv, substr($ciphertext, 0, $i), $orig_pos, $i);
- 1110 $ciphertext = substr($ciphertext, $i);
- 1111 }
- 1112 $overflow = $len % $this->block_size;
- 1113 if ($overflow) {
- 1114 $plaintext.= openssl_decrypt(substr($ciphertext, 0, -$overflow), $this->cipher_name_openssl, $this->key, $this->openssl_options, $iv);
- 1115 if ($len - $overflow) {
- 1116 $iv = substr($ciphertext, -$overflow - $this->block_size, -$overflow);
- 1117 }
- 1118 $iv = openssl_encrypt(str_repeat("\0", $this->block_size), $this->cipher_name_openssl, $this->key, $this->openssl_options, $iv);
- 1119 $plaintext.= $iv ^ substr($ciphertext, -$overflow);
- 1120 $iv = substr_replace($iv, substr($ciphertext, -$overflow), 0, $overflow);
- 1121 $pos = $overflow;
- 1122 } elseif ($len) {
- 1123 $plaintext.= openssl_decrypt($ciphertext, $this->cipher_name_openssl, $this->key, $this->openssl_options, $iv);
- 1124 $iv = substr($ciphertext, -$this->block_size);
- 1125 }
- 1126 break;
- 1127 case CRYPT_MODE_OFB:
- 1128 $plaintext = $this->_openssl_ofb_process($ciphertext, $this->decryptIV, $this->debuffer);
- 1129 }
- 1130
- 1131 return $this->paddable ? $this->_unpad($plaintext) : $plaintext;
- 1132 }
- 1133
- 1134 if ($this->engine === CRYPT_ENGINE_MCRYPT) {
- 1135 $block_size = $this->block_size;
- 1136 if ($this->changed) {
- 1137 $this->_setupMcrypt();
- 1138 $this->changed = false;
- 1139 }
- 1140 if ($this->dechanged) {
- 1141 @mcrypt_generic_init($this->demcrypt, $this->key, $this->decryptIV);
- 1142 $this->dechanged = false;
- 1143 }
- 1144
- 1145 if ($this->mode == CRYPT_MODE_CFB && $this->continuousBuffer) {
- 1146 $iv = &$this->decryptIV;
- 1147 $pos = &$this->debuffer['pos'];
- 1148 $len = strlen($ciphertext);
- 1149 $plaintext = '';
- 1150 $i = 0;
- 1151 if ($pos) {
- 1152 $orig_pos = $pos;
- 1153 $max = $block_size - $pos;
- 1154 if ($len >= $max) {
- 1155 $i = $max;
- 1156 $len-= $max;
- 1157 $pos = 0;
- 1158 } else {
- 1159 $i = $len;
- 1160 $pos+= $len;
- 1161 $len = 0;
- 1162 }
- 1163
- 1164 $plaintext = substr($iv, $orig_pos) ^ $ciphertext;
- 1165 $iv = substr_replace($iv, substr($ciphertext, 0, $i), $orig_pos, $i);
- 1166 }
- 1167 if ($len >= $block_size) {
- 1168 $cb = substr($ciphertext, $i, $len - $len % $block_size);
- 1169 $plaintext.= @mcrypt_generic($this->ecb, $iv . $cb) ^ $cb;
- 1170 $iv = substr($cb, -$block_size);
- 1171 $len%= $block_size;
- 1172 }
- 1173 if ($len) {
- 1174 $iv = @mcrypt_generic($this->ecb, $iv);
- 1175 $plaintext.= $iv ^ substr($ciphertext, -$len);
- 1176 $iv = substr_replace($iv, substr($ciphertext, -$len), 0, $len);
- 1177 $pos = $len;
- 1178 }
- 1179
- 1180 return $plaintext;
- 1181 }
- 1182
- 1183 $plaintext = @mdecrypt_generic($this->demcrypt, $ciphertext);
- 1184
- 1185 if (!$this->continuousBuffer) {
- 1186 @mcrypt_generic_init($this->demcrypt, $this->key, $this->decryptIV);
- 1187 }
- 1188
- 1189 return $this->paddable ? $this->_unpad($plaintext) : $plaintext;
- 1190 }
- 1191
- 1192 if ($this->changed) {
- 1193 $this->_setup();
- 1194 $this->changed = false;
- 1195 }
- 1196 if ($this->use_inline_crypt) {
- 1197 $inline = $this->inline_crypt;
- 1198 return $inline('decrypt', $this, $ciphertext);
- 1199 }
- 1200
- 1201 $block_size = $this->block_size;
- 1202
- 1203 $buffer = &$this->debuffer;
- 1204 $plaintext = '';
- 1205 switch ($this->mode) {
- 1206 case CRYPT_MODE_ECB:
- 1207 for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
- 1208 $plaintext.= $this->_decryptBlock(substr($ciphertext, $i, $block_size));
- 1209 }
- 1210 break;
- 1211 case CRYPT_MODE_CBC:
- 1212 $xor = $this->decryptIV;
- 1213 for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
- 1214 $block = substr($ciphertext, $i, $block_size);
- 1215 $plaintext.= $this->_decryptBlock($block) ^ $xor;
- 1216 $xor = $block;
- 1217 }
- 1218 if ($this->continuousBuffer) {
- 1219 $this->decryptIV = $xor;
- 1220 }
- 1221 break;
- 1222 case CRYPT_MODE_CTR:
- 1223 $xor = $this->decryptIV;
- 1224 if (strlen($buffer['ciphertext'])) {
- 1225 for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
- 1226 $block = substr($ciphertext, $i, $block_size);
- 1227 if (strlen($block) > strlen($buffer['ciphertext'])) {
- 1228 $buffer['ciphertext'].= $this->_encryptBlock($xor);
- 1229 $this->_increment_str($xor);
- 1230 }
- 1231 $key = $this->_string_shift($buffer['ciphertext'], $block_size);
- 1232 $plaintext.= $block ^ $key;
- 1233 }
- 1234 } else {
- 1235 for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
- 1236 $block = substr($ciphertext, $i, $block_size);
- 1237 $key = $this->_encryptBlock($xor);
- 1238 $this->_increment_str($xor);
- 1239 $plaintext.= $block ^ $key;
- 1240 }
- 1241 }
- 1242 if ($this->continuousBuffer) {
- 1243 $this->decryptIV = $xor;
- 1244 if ($start = strlen($ciphertext) % $block_size) {
- 1245 $buffer['ciphertext'] = substr($key, $start) . $buffer['ciphertext'];
- 1246 }
- 1247 }
- 1248 break;
- 1249 case CRYPT_MODE_CFB:
- 1250 if ($this->continuousBuffer) {
- 1251 $iv = &$this->decryptIV;
- 1252 $pos = &$buffer['pos'];
- 1253 } else {
- 1254 $iv = $this->decryptIV;
- 1255 $pos = 0;
- 1256 }
- 1257 $len = strlen($ciphertext);
- 1258 $i = 0;
- 1259 if ($pos) {
- 1260 $orig_pos = $pos;
- 1261 $max = $block_size - $pos;
- 1262 if ($len >= $max) {
- 1263 $i = $max;
- 1264 $len-= $max;
- 1265 $pos = 0;
- 1266 } else {
- 1267 $i = $len;
- 1268 $pos+= $len;
- 1269 $len = 0;
- 1270 }
- 1271
- 1272 $plaintext = substr($iv, $orig_pos) ^ $ciphertext;
- 1273 $iv = substr_replace($iv, substr($ciphertext, 0, $i), $orig_pos, $i);
- 1274 }
- 1275 while ($len >= $block_size) {
- 1276 $iv = $this->_encryptBlock($iv);
- 1277 $cb = substr($ciphertext, $i, $block_size);
- 1278 $plaintext.= $iv ^ $cb;
- 1279 $iv = $cb;
- 1280 $len-= $block_size;
- 1281 $i+= $block_size;
- 1282 }
- 1283 if ($len) {
- 1284 $iv = $this->_encryptBlock($iv);
- 1285 $plaintext.= $iv ^ substr($ciphertext, $i);
- 1286 $iv = substr_replace($iv, substr($ciphertext, $i), 0, $len);
- 1287 $pos = $len;
- 1288 }
- 1289 break;
- 1290 case CRYPT_MODE_OFB:
- 1291 $xor = $this->decryptIV;
- 1292 if (strlen($buffer['xor'])) {
- 1293 for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
- 1294 $block = substr($ciphertext, $i, $block_size);
- 1295 if (strlen($block) > strlen($buffer['xor'])) {
- 1296 $xor = $this->_encryptBlock($xor);
- 1297 $buffer['xor'].= $xor;
- 1298 }
- 1299 $key = $this->_string_shift($buffer['xor'], $block_size);
- 1300 $plaintext.= $block ^ $key;
- 1301 }
- 1302 } else {
- 1303 for ($i = 0; $i < strlen($ciphertext); $i+=$block_size) {
- 1304 $xor = $this->_encryptBlock($xor);
- 1305 $plaintext.= substr($ciphertext, $i, $block_size) ^ $xor;
- 1306 }
- 1307 $key = $xor;
- 1308 }
- 1309 if ($this->continuousBuffer) {
- 1310 $this->decryptIV = $xor;
- 1311 if ($start = strlen($ciphertext) % $block_size) {
- 1312 $buffer['xor'] = substr($key, $start) . $buffer['xor'];
- 1313 }
- 1314 }
- 1315 break;
- 1316 case CRYPT_MODE_STREAM:
- 1317 $plaintext = $this->_decryptBlock($ciphertext);
- 1318 break;
- 1319 }
- 1320 return $this->paddable ? $this->_unpad($plaintext) : $plaintext;
- 1321 }
- 1322
- 1323
- 1324
- 1325
- 1326
- 1327
- 1328
- 1329
- 1330
- 1331
- 1332
- 1333
- 1334
- 1335
- 1336
- 1337
- 1338
- 1339 function _openssl_ctr_process($plaintext, &$encryptIV, &$buffer)
- 1340 {
- 1341 $ciphertext = '';
- 1342
- 1343 $block_size = $this->block_size;
- 1344 $key = $this->key;
- 1345
- 1346 if ($this->openssl_emulate_ctr) {
- 1347 $xor = $encryptIV;
- 1348 if (strlen($buffer['ciphertext'])) {
- 1349 for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
- 1350 $block = substr($plaintext, $i, $block_size);
- 1351 if (strlen($block) > strlen($buffer['ciphertext'])) {
- 1352 $result = openssl_encrypt($xor, $this->cipher_name_openssl_ecb, $key, $this->openssl_options);
- 1353 $result = !defined('OPENSSL_RAW_DATA') ? substr($result, 0, -$this->block_size) : $result;
- 1354 $buffer['ciphertext'].= $result;
- 1355 }
- 1356 $this->_increment_str($xor);
- 1357 $otp = $this->_string_shift($buffer['ciphertext'], $block_size);
- 1358 $ciphertext.= $block ^ $otp;
- 1359 }
- 1360 } else {
- 1361 for ($i = 0; $i < strlen($plaintext); $i+=$block_size) {
- 1362 $block = substr($plaintext, $i, $block_size);
- 1363 $otp = openssl_encrypt($xor, $this->cipher_name_openssl_ecb, $key, $this->openssl_options);
- 1364 $otp = !defined('OPENSSL_RAW_DATA') ? substr($otp, 0, -$this->block_size) : $otp;
- 1365 $this->_increment_str($xor);
- 1366 $ciphertext.= $block ^ $otp;
- 1367 }
- 1368 }
- 1369 if ($this->continuousBuffer) {
- 1370 $encryptIV = $xor;
- 1371 if ($start = strlen($plaintext) % $block_size) {
- 1372 $buffer['ciphertext'] = substr($key, $start) . $buffer['ciphertext'];
- 1373 }
- 1374 }
- 1375
- 1376 return $ciphertext;
- 1377 }
- 1378
- 1379 if (strlen($buffer['ciphertext'])) {
- 1380 $ciphertext = $plaintext ^ $this->_string_shift($buffer['ciphertext'], strlen($plaintext));
- 1381 $plaintext = substr($plaintext, strlen($ciphertext));
- 1382
- 1383 if (!strlen($plaintext)) {
- 1384 return $ciphertext;
- 1385 }
- 1386 }
- 1387
- 1388 $overflow = strlen($plaintext) % $block_size;
- 1389 if ($overflow) {
- 1390 $plaintext2 = $this->_string_pop($plaintext, $overflow);
- 1391 $encrypted = openssl_encrypt($plaintext . str_repeat("\0", $block_size), $this->cipher_name_openssl, $key, $this->openssl_options, $encryptIV);
- 1392 $temp = $this->_string_pop($encrypted, $block_size);
- 1393 $ciphertext.= $encrypted . ($plaintext2 ^ $temp);
- 1394 if ($this->continuousBuffer) {
- 1395 $buffer['ciphertext'] = substr($temp, $overflow);
- 1396 $encryptIV = $temp;
- 1397 }
- 1398 } elseif (!strlen($buffer['ciphertext'])) {
- 1399 $ciphertext.= openssl_encrypt($plaintext . str_repeat("\0", $block_size), $this->cipher_name_openssl, $key, $this->openssl_options, $encryptIV);
- 1400 $temp = $this->_string_pop($ciphertext, $block_size);
- 1401 if ($this->continuousBuffer) {
- 1402 $encryptIV = $temp;
- 1403 }
- 1404 }
- 1405 if ($this->continuousBuffer) {
- 1406 if (!defined('OPENSSL_RAW_DATA')) {
- 1407 $encryptIV.= openssl_encrypt('', $this->cipher_name_openssl_ecb, $key, $this->openssl_options);
- 1408 }
- 1409 $encryptIV = openssl_decrypt($encryptIV, $this->cipher_name_openssl_ecb, $key, $this->openssl_options);
- 1410 if ($overflow) {
- 1411 $this->_increment_str($encryptIV);
- 1412 }
- 1413 }
- 1414
- 1415 return $ciphertext;
- 1416 }
- 1417
- 1418
- 1419
- 1420
- 1421
- 1422
- 1423
- 1424
- 1425
- 1426
- 1427
- 1428
- 1429
- 1430
- 1431
- 1432
- 1433 function _openssl_ofb_process($plaintext, &$encryptIV, &$buffer)
- 1434 {
- 1435 if (strlen($buffer['xor'])) {
- 1436 $ciphertext = $plaintext ^ $buffer['xor'];
- 1437 $buffer['xor'] = substr($buffer['xor'], strlen($ciphertext));
- 1438 $plaintext = substr($plaintext, strlen($ciphertext));
- 1439 } else {
- 1440 $ciphertext = '';
- 1441 }
- 1442
- 1443 $block_size = $this->block_size;
- 1444
- 1445 $len = strlen($plaintext);
- 1446 $key = $this->key;
- 1447 $overflow = $len % $block_size;
- 1448
- 1449 if (strlen($plaintext)) {
- 1450 if ($overflow) {
- 1451 $ciphertext.= openssl_encrypt(substr($plaintext, 0, -$overflow) . str_repeat("\0", $block_size), $this->cipher_name_openssl, $key, $this->openssl_options, $encryptIV);
- 1452 $xor = $this->_string_pop($ciphertext, $block_size);
- 1453 if ($this->continuousBuffer) {
- 1454 $encryptIV = $xor;
- 1455 }
- 1456 $ciphertext.= $this->_string_shift($xor, $overflow) ^ substr($plaintext, -$overflow);
- 1457 if ($this->continuousBuffer) {
- 1458 $buffer['xor'] = $xor;
- 1459 }
- 1460 } else {
- 1461 $ciphertext = openssl_encrypt($plaintext, $this->cipher_name_openssl, $key, $this->openssl_options, $encryptIV);
- 1462 if ($this->continuousBuffer) {
- 1463 $encryptIV = substr($ciphertext, -$block_size) ^ substr($plaintext, -$block_size);
- 1464 }
- 1465 }
- 1466 }
- 1467
- 1468 return $ciphertext;
- 1469 }
- 1470
- 1471
- 1472
- 1473
- 1474
- 1475
- 1476
- 1477
- 1478
- 1479 function _openssl_translate_mode()
- 1480 {
- 1481 switch ($this->mode) {
- 1482 case CRYPT_MODE_ECB:
- 1483 return 'ecb';
- 1484 case CRYPT_MODE_CBC:
- 1485 return 'cbc';
- 1486 case CRYPT_MODE_CTR:
- 1487 return 'ctr';
- 1488 case CRYPT_MODE_CFB:
- 1489 return 'cfb';
- 1490 case CRYPT_MODE_OFB:
- 1491 return 'ofb';
- 1492 }
- 1493 }
- 1494
- 1495
- 1496
- 1497
- 1498
- 1499
- 1500
- 1501
- 1502
- 1503
- 1504
- 1505
- 1506
- 1507
- 1508
- 1509
- 1510 function enablePadding()
- 1511 {
- 1512 $this->padding = true;
- 1513 }
- 1514
- 1515
- 1516
- 1517
- 1518
- 1519
- 1520
- 1521 function disablePadding()
- 1522 {
- 1523 $this->padding = false;
- 1524 }
- 1525
- 1526
- 1527
- 1528
- 1529
- 1530
- 1531
- 1532
- 1533
- 1534
- 1535
- 1536
- 1537
- 1538
- 1539
- 1540
- 1541
- 1542
- 1543
- 1544
- 1545
- 1546
- 1547
- 1548
- 1549
- 1550
- 1551
- 1552
- 1553
- 1554
- 1555
- 1556
- 1557
- 1558
- 1559
- 1560
- 1561
- 1562
- 1563
- 1564 function enableContinuousBuffer()
- 1565 {
- 1566 if ($this->mode == CRYPT_MODE_ECB) {
- 1567 return;
- 1568 }
- 1569
- 1570 $this->continuousBuffer = true;
- 1571
- 1572 $this->_setEngine();
- 1573 }
- 1574
- 1575
- 1576
- 1577
- 1578
- 1579
- 1580
- 1581
- 1582
- 1583
- 1584 function disableContinuousBuffer()
- 1585 {
- 1586 if ($this->mode == CRYPT_MODE_ECB) {
- 1587 return;
- 1588 }
- 1589 if (!$this->continuousBuffer) {
- 1590 return;
- 1591 }
- 1592
- 1593 $this->continuousBuffer = false;
- 1594 $this->changed = true;
- 1595
- 1596 $this->_setEngine();
- 1597 }
- 1598
- 1599
- 1600
- 1601
- 1602
- 1603
- 1604
- 1605
- 1606
- 1607 function isValidEngine($engine)
- 1608 {
- 1609 switch ($engine) {
- 1610 case CRYPT_ENGINE_OPENSSL:
- 1611 if ($this->mode == CRYPT_MODE_STREAM && $this->continuousBuffer) {
- 1612 return false;
- 1613 }
- 1614 $this->openssl_emulate_ctr = false;
- 1615 $result = $this->cipher_name_openssl &&
- 1616 extension_loaded('openssl') &&
- 1617
- 1618 version_compare(PHP_VERSION, '5.3.3', '>=');
- 1619 if (!$result) {
- 1620 return false;
- 1621 }
- 1622
- 1623
- 1624
- 1625 if (!defined('OPENSSL_RAW_DATA')) {
- 1626 $this->openssl_options = true;
- 1627 } else {
- 1628 $this->openssl_options = OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING;
- 1629 }
- 1630
- 1631 $methods = openssl_get_cipher_methods();
- 1632 if (in_array($this->cipher_name_openssl, $methods)) {
- 1633 return true;
- 1634 }
- 1635
- 1636
- 1637 switch ($this->mode) {
- 1638 case CRYPT_MODE_CTR:
- 1639 if (in_array($this->cipher_name_openssl_ecb, $methods)) {
- 1640 $this->openssl_emulate_ctr = true;
- 1641 return true;
- 1642 }
- 1643 }
- 1644 return false;
- 1645 case CRYPT_ENGINE_MCRYPT:
- 1646 return $this->cipher_name_mcrypt &&
- 1647 extension_loaded('mcrypt') &&
- 1648 in_array($this->cipher_name_mcrypt, @mcrypt_list_algorithms());
- 1649 case CRYPT_ENGINE_INTERNAL:
- 1650 return true;
- 1651 }
- 1652
- 1653 return false;
- 1654 }
- 1655
- 1656
- 1657
- 1658
- 1659
- 1660
- 1661
- 1662
- 1663
- 1664
- 1665
- 1666
- 1667
- 1668
- 1669
- 1670
- 1671
- 1672
- 1673 function setPreferredEngine($engine)
- 1674 {
- 1675 switch ($engine) {
- 1676
- 1677 case CRYPT_ENGINE_MCRYPT:
- 1678 case CRYPT_ENGINE_INTERNAL:
- 1679 $this->preferredEngine = $engine;
- 1680 break;
- 1681 default:
- 1682 $this->preferredEngine = CRYPT_ENGINE_OPENSSL;
- 1683 }
- 1684
- 1685 $this->_setEngine();
- 1686 }
- 1687
- 1688
- 1689
- 1690
- 1691
- 1692
- 1693
- 1694 function getEngine()
- 1695 {
- 1696 return $this->engine;
- 1697 }
- 1698
- 1699
- 1700
- 1701
- 1702
- 1703
- 1704
- 1705 function _setEngine()
- 1706 {
- 1707 $this->engine = null;
- 1708
- 1709 $candidateEngines = array(
- 1710 $this->preferredEngine,
- 1711 CRYPT_ENGINE_OPENSSL,
- 1712 CRYPT_ENGINE_MCRYPT
- 1713 );
- 1714 foreach ($candidateEngines as $engine) {
- 1715 if ($this->isValidEngine($engine)) {
- 1716 $this->engine = $engine;
- 1717 break;
- 1718 }
- 1719 }
- 1720 if (!$this->engine) {
- 1721 $this->engine = CRYPT_ENGINE_INTERNAL;
- 1722 }
- 1723
- 1724 if ($this->engine != CRYPT_ENGINE_MCRYPT && $this->enmcrypt) {
- 1725
- 1726
- 1727 @mcrypt_module_close($this->enmcrypt);
- 1728 @mcrypt_module_close($this->demcrypt);
- 1729 $this->enmcrypt = null;
- 1730 $this->demcrypt = null;
- 1731
- 1732 if ($this->ecb) {
- 1733 @mcrypt_module_close($this->ecb);
- 1734 $this->ecb = null;
- 1735 }
- 1736 }
- 1737
- 1738 $this->changed = true;
- 1739 }
- 1740
- 1741
- 1742
- 1743
- 1744
- 1745
- 1746
- 1747
- 1748
- 1749 function _encryptBlock($in)
- 1750 {
- 1751 user_error((version_compare(PHP_VERSION, '5.0.0', '>=') ? __METHOD__ : __FUNCTION__) . '() must extend by class ' . get_class($this), E_USER_ERROR);
- 1752 }
- 1753
- 1754
- 1755
- 1756
- 1757
- 1758
- 1759
- 1760
- 1761
- 1762 function _decryptBlock($in)
- 1763 {
- 1764 user_error((version_compare(PHP_VERSION, '5.0.0', '>=') ? __METHOD__ : __FUNCTION__) . '() must extend by class ' . get_class($this), E_USER_ERROR);
- 1765 }
- 1766
- 1767
- 1768
- 1769
- 1770
- 1771
- 1772
- 1773
- 1774
- 1775
- 1776 function _setupKey()
- 1777 {
- 1778 user_error((version_compare(PHP_VERSION, '5.0.0', '>=') ? __METHOD__ : __FUNCTION__) . '() must extend by class ' . get_class($this), E_USER_ERROR);
- 1779 }
- 1780
- 1781
- 1782
- 1783
- 1784
- 1785
- 1786
- 1787
- 1788
- 1789
- 1790
- 1791
- 1792
- 1793
- 1794
- 1795
- 1796
- 1797
- 1798
- 1799
- 1800
- 1801
- 1802
- 1803
- 1804
- 1805 function _setup()
- 1806 {
- 1807 $this->_clearBuffers();
- 1808 $this->_setupKey();
- 1809
- 1810 if ($this->use_inline_crypt) {
- 1811 $this->_setupInlineCrypt();
- 1812 }
- 1813 }
- 1814
- 1815
- 1816
- 1817
- 1818
- 1819
- 1820
- 1821
- 1822
- 1823
- 1824
- 1825
- 1826
- 1827
- 1828
- 1829
- 1830
- 1831
- 1832
- 1833
- 1834
- 1835
- 1836
- 1837
- 1838 function _setupMcrypt()
- 1839 {
- 1840 $this->_clearBuffers();
- 1841 $this->enchanged = $this->dechanged = true;
- 1842
- 1843 if (!isset($this->enmcrypt)) {
- 1844 static $mcrypt_modes = array(
- 1845 CRYPT_MODE_CTR => 'ctr',
- 1846 CRYPT_MODE_ECB => MCRYPT_MODE_ECB,
- 1847 CRYPT_MODE_CBC => MCRYPT_MODE_CBC,
- 1848 CRYPT_MODE_CFB => 'ncfb',
- 1849 CRYPT_MODE_OFB => MCRYPT_MODE_NOFB,
- 1850 CRYPT_MODE_STREAM => MCRYPT_MODE_STREAM,
- 1851 );
- 1852
- 1853 $this->demcrypt = @mcrypt_module_open($this->cipher_name_mcrypt, '', $mcrypt_modes[$this->mode], '');
- 1854 $this->enmcrypt = @mcrypt_module_open($this->cipher_name_mcrypt, '', $mcrypt_modes[$this->mode], '');
- 1855
- 1856
- 1857
- 1858
- 1859 if ($this->mode == CRYPT_MODE_CFB) {
- 1860 $this->ecb = @mcrypt_module_open($this->cipher_name_mcrypt, '', MCRYPT_MODE_ECB, '');
- 1861 }
- 1862 }
- 1863
- 1864 if ($this->mode == CRYPT_MODE_CFB) {
- 1865 @mcrypt_generic_init($this->ecb, $this->key, str_repeat("\0", $this->block_size));
- 1866 }
- 1867 }
- 1868
- 1869
- 1870
- 1871
- 1872
- 1873
- 1874
- 1875
- 1876
- 1877
- 1878
- 1879
- 1880
- 1881
- 1882
- 1883
- 1884 function _pad($text)
- 1885 {
- 1886 $length = strlen($text);
- 1887
- 1888 if (!$this->padding) {
- 1889 if ($length % $this->block_size == 0) {
- 1890 return $text;
- 1891 } else {
- 1892 user_error("The plaintext's length ($length) is not a multiple of the block size ({$this->block_size})");
- 1893 $this->padding = true;
- 1894 }
- 1895 }
- 1896
- 1897 $pad = $this->block_size - ($length % $this->block_size);
- 1898
- 1899 return str_pad($text, $length + $pad, chr($pad));
- 1900 }
- 1901
- 1902
- 1903
- 1904
- 1905
- 1906
- 1907
- 1908
- 1909
- 1910
- 1911
- 1912
- 1913 function _unpad($text)
- 1914 {
- 1915 if (!$this->padding) {
- 1916 return $text;
- 1917 }
- 1918
- 1919 $length = ord($text[strlen($text) - 1]);
- 1920
- 1921 if (!$length || $length > $this->block_size) {
- 1922 return false;
- 1923 }
- 1924
- 1925 return substr($text, 0, -$length);
- 1926 }
- 1927
- 1928
- 1929
- 1930
- 1931
- 1932
- 1933
- 1934
- 1935
- 1936
- 1937
- 1938 function _clearBuffers()
- 1939 {
- 1940 $this->enbuffer = $this->debuffer = array('ciphertext' => '', 'xor' => '', 'pos' => 0, 'enmcrypt_init' => true);
- 1941
- 1942
- 1943
- 1944 $this->encryptIV = $this->decryptIV = str_pad(substr($this->iv, 0, $this->block_size), $this->block_size, "\0");
- 1945
- 1946 if (!$this->skip_key_adjustment) {
- 1947 $this->key = str_pad(substr($this->key, 0, $this->key_length), $this->key_length, "\0");
- 1948 }
- 1949 }
- 1950
- 1951
- 1952
- 1953
- 1954
- 1955
- 1956
- 1957
- 1958
- 1959
- 1960
- 1961 function _string_shift(&$string, $index = 1)
- 1962 {
- 1963 $substr = substr($string, 0, $index);
- 1964 $string = substr($string, $index);
- 1965 return $substr;
- 1966 }
- 1967
- 1968
- 1969
- 1970
- 1971
- 1972
- 1973
- 1974
- 1975
- 1976
- 1977
- 1978 function _string_pop(&$string, $index = 1)
- 1979 {
- 1980 $substr = substr($string, -$index);
- 1981 $string = substr($string, 0, -$index);
- 1982 return $substr;
- 1983 }
- 1984
- 1985
- 1986
- 1987
- 1988
- 1989
- 1990
- 1991
- 1992
- 1993 function _increment_str(&$var)
- 1994 {
- 1995 for ($i = 4; $i <= strlen($var); $i+= 4) {
- 1996 $temp = substr($var, -$i, 4);
- 1997 switch ($temp) {
- 1998 case "\xFF\xFF\xFF\xFF":
- 1999 $var = substr_replace($var, "\x00\x00\x00\x00", -$i, 4);
- 2000 break;
- 2001 case "\x7F\xFF\xFF\xFF":
- 2002 $var = substr_replace($var, "\x80\x00\x00\x00", -$i, 4);
- 2003 return;
- 2004 default:
- 2005 $temp = unpack('Nnum', $temp);
- 2006 $var = substr_replace($var, pack('N', $temp['num'] + 1), -$i, 4);
- 2007 return;
- 2008 }
- 2009 }
- 2010
- 2011 $remainder = strlen($var) % 4;
- 2012
- 2013 if ($remainder == 0) {
- 2014 return;
- 2015 }
- 2016
- 2017 $temp = unpack('Nnum', str_pad(substr($var, 0, $remainder), 4, "\0", STR_PAD_LEFT));
- 2018 $temp = substr(pack('N', $temp['num'] + 1), -$remainder);
- 2019 $var = substr_replace($var, $temp, 0, $remainder);
- 2020 }
- 2021
- 2022
- 2023
- 2024
- 2025
- 2026
- 2027
- 2028
- 2029
- 2030
- 2031
- 2032
- 2033
- 2034
- 2035
- 2036
- 2037
- 2038
- 2039
- 2040
- 2041
- 2042
- 2043
- 2044
- 2045
- 2046
- 2047
- 2048
- 2049
- 2050
- 2051
- 2052
- 2053
- 2054
- 2055
- 2056
- 2057
- 2058
- 2059
- 2060
- 2061
- 2062
- 2063
- 2064
- 2065
- 2066
- 2067
- 2068
- 2069
- 2070
- 2071
- 2072
- 2073
- 2074
- 2075
- 2076
- 2077
- 2078
- 2079
- 2080
- 2081
- 2082 function _setupInlineCrypt()
- 2083 {
- 2084
- 2085
- 2086
- 2087
- 2088
- 2089
- 2090 $this->use_inline_crypt = false;
- 2091 }
- 2092
- 2093
- 2094
- 2095
- 2096
- 2097
- 2098
- 2099
- 2100
- 2101
- 2102
- 2103
- 2104
- 2105
- 2106
- 2107
- 2108
- 2109
- 2110
- 2111
- 2112
- 2113
- 2114
- 2115
- 2116
- 2117
- 2118
- 2119
- 2120
- 2121
- 2122
- 2123
- 2124
- 2125
- 2126
- 2127
- 2128
- 2129
- 2130
- 2131
- 2132
- 2133
- 2134
- 2135
- 2136
- 2137
- 2138
- 2139
- 2140
- 2141
- 2142
- 2143
- 2144
- 2145
- 2146
- 2147
- 2148
- 2149
- 2150
- 2151
- 2152
- 2153
- 2154
- 2155
- 2156
- 2157
- 2158
- 2159
- 2160
- 2161
- 2162
- 2163
- 2164
- 2165
- 2166
- 2167
- 2168
- 2169
- 2170
- 2171
- 2172
- 2173
- 2174
- 2175
- 2176
- 2177
- 2178
- 2179
- 2180
- 2181
- 2182
- 2183
- 2184
- 2185
- 2186
- 2187
- 2188
- 2189
- 2190
- 2191
- 2192
- 2193
- 2194
- 2195
- 2196
- 2197
- 2198
- 2199
- 2200
- 2201
- 2202
- 2203
- 2204 function _createInlineCryptFunction($cipher_code)
- 2205 {
- 2206 $block_size = $this->block_size;
- 2207
- 2208
- 2209 $init_crypt = isset($cipher_code['init_crypt']) ? $cipher_code['init_crypt'] : '';
- 2210 $init_encrypt = isset($cipher_code['init_encrypt']) ? $cipher_code['init_encrypt'] : '';
- 2211 $init_decrypt = isset($cipher_code['init_decrypt']) ? $cipher_code['init_decrypt'] : '';
- 2212
- 2213 $encrypt_block = $cipher_code['encrypt_block'];
- 2214 $decrypt_block = $cipher_code['decrypt_block'];
- 2215
- 2216
- 2217
- 2218
- 2219 switch ($this->mode) {
- 2220 case CRYPT_MODE_ECB:
- 2221 $encrypt = $init_encrypt . '
- 2222 $_ciphertext = "";
- 2223 $_plaintext_len = strlen($_text);
- 2224
- 2225 for ($_i = 0; $_i < $_plaintext_len; $_i+= '.$block_size.') {
- 2226 $in = substr($_text, $_i, '.$block_size.');
- 2227 '.$encrypt_block.'
- 2228 $_ciphertext.= $in;
- 2229 }
- 2230
- 2231 return $_ciphertext;
- 2232 ';
- 2233
- 2234 $decrypt = $init_decrypt . '
- 2235 $_plaintext = "";
- 2236 $_text = str_pad($_text, strlen($_text) + ('.$block_size.' - strlen($_text) % '.$block_size.') % '.$block_size.', chr(0));
- 2237 $_ciphertext_len = strlen($_text);
- 2238
- 2239 for ($_i = 0; $_i < $_ciphertext_len; $_i+= '.$block_size.') {
- 2240 $in = substr($_text, $_i, '.$block_size.');
- 2241 '.$decrypt_block.'
- 2242 $_plaintext.= $in;
- 2243 }
- 2244
- 2245 return $self->_unpad($_plaintext);
- 2246 ';
- 2247 break;
- 2248 case CRYPT_MODE_CTR:
- 2249 $encrypt = $init_encrypt . '
- 2250 $_ciphertext = "";
- 2251 $_plaintext_len = strlen($_text);
- 2252 $_xor = $self->encryptIV;
- 2253 $_buffer = &$self->enbuffer;
- 2254 if (strlen($_buffer["ciphertext"])) {
- 2255 for ($_i = 0; $_i < $_plaintext_len; $_i+= '.$block_size.') {
- 2256 $_block = substr($_text, $_i, '.$block_size.');
- 2257 if (strlen($_block) > strlen($_buffer["ciphertext"])) {
- 2258 $in = $_xor;
- 2259 '.$encrypt_block.'
- 2260 $self->_increment_str($_xor);
- 2261 $_buffer["ciphertext"].= $in;
- 2262 }
- 2263 $_key = $self->_string_shift($_buffer["ciphertext"], '.$block_size.');
- 2264 $_ciphertext.= $_block ^ $_key;
- 2265 }
- 2266 } else {
- 2267 for ($_i = 0; $_i < $_plaintext_len; $_i+= '.$block_size.') {
- 2268 $_block = substr($_text, $_i, '.$block_size.');
- 2269 $in = $_xor;
- 2270 '.$encrypt_block.'
- 2271 $self->_increment_str($_xor);
- 2272 $_key = $in;
- 2273 $_ciphertext.= $_block ^ $_key;
- 2274 }
- 2275 }
- 2276 if ($self->continuousBuffer) {
- 2277 $self->encryptIV = $_xor;
- 2278 if ($_start = $_plaintext_len % '.$block_size.') {
- 2279 $_buffer["ciphertext"] = substr($_key, $_start) . $_buffer["ciphertext"];
- 2280 }
- 2281 }
- 2282
- 2283 return $_ciphertext;
- 2284 ';
- 2285
- 2286 $decrypt = $init_encrypt . '
- 2287 $_plaintext = "";
- 2288 $_ciphertext_len = strlen($_text);
- 2289 $_xor = $self->decryptIV;
- 2290 $_buffer = &$self->debuffer;
- 2291
- 2292 if (strlen($_buffer["ciphertext"])) {
- 2293 for ($_i = 0; $_i < $_ciphertext_len; $_i+= '.$block_size.') {
- 2294 $_block = substr($_text, $_i, '.$block_size.');
- 2295 if (strlen($_block) > strlen($_buffer["ciphertext"])) {
- 2296 $in = $_xor;
- 2297 '.$encrypt_block.'
- 2298 $self->_increment_str($_xor);
- 2299 $_buffer["ciphertext"].= $in;
- 2300 }
- 2301 $_key = $self->_string_shift($_buffer["ciphertext"], '.$block_size.');
- 2302 $_plaintext.= $_block ^ $_key;
- 2303 }
- 2304 } else {
- 2305 for ($_i = 0; $_i < $_ciphertext_len; $_i+= '.$block_size.') {
- 2306 $_block = substr($_text, $_i, '.$block_size.');
- 2307 $in = $_xor;
- 2308 '.$encrypt_block.'
- 2309 $self->_increment_str($_xor);
- 2310 $_key = $in;
- 2311 $_plaintext.= $_block ^ $_key;
- 2312 }
- 2313 }
- 2314 if ($self->continuousBuffer) {
- 2315 $self->decryptIV = $_xor;
- 2316 if ($_start = $_ciphertext_len % '.$block_size.') {
- 2317 $_buffer["ciphertext"] = substr($_key, $_start) . $_buffer["ciphertext"];
- 2318 }
- 2319 }
- 2320
- 2321 return $_plaintext;
- 2322 ';
- 2323 break;
- 2324 case CRYPT_MODE_CFB:
- 2325 $encrypt = $init_encrypt . '
- 2326 $_ciphertext = "";
- 2327 $_buffer = &$self->enbuffer;
- 2328
- 2329 if ($self->continuousBuffer) {
- 2330 $_iv = &$self->encryptIV;
- 2331 $_pos = &$_buffer["pos"];
- 2332 } else {
- 2333 $_iv = $self->encryptIV;
- 2334 $_pos = 0;
- 2335 }
- 2336 $_len = strlen($_text);
- 2337 $_i = 0;
- 2338 if ($_pos) {
- 2339 $_orig_pos = $_pos;
- 2340 $_max = '.$block_size.' - $_pos;
- 2341 if ($_len >= $_max) {
- 2342 $_i = $_max;
- 2343 $_len-= $_max;
- 2344 $_pos = 0;
- 2345 } else {
- 2346 $_i = $_len;
- 2347 $_pos+= $_len;
- 2348 $_len = 0;
- 2349 }
- 2350 $_ciphertext = substr($_iv, $_orig_pos) ^ $_text;
- 2351 $_iv = substr_replace($_iv, $_ciphertext, $_orig_pos, $_i);
- 2352 }
- 2353 while ($_len >= '.$block_size.') {
- 2354 $in = $_iv;
- 2355 '.$encrypt_block.';
- 2356 $_iv = $in ^ substr($_text, $_i, '.$block_size.');
- 2357 $_ciphertext.= $_iv;
- 2358 $_len-= '.$block_size.';
- 2359 $_i+= '.$block_size.';
- 2360 }
- 2361 if ($_len) {
- 2362 $in = $_iv;
- 2363 '.$encrypt_block.'
- 2364 $_iv = $in;
- 2365 $_block = $_iv ^ substr($_text, $_i);
- 2366 $_iv = substr_replace($_iv, $_block, 0, $_len);
- 2367 $_ciphertext.= $_block;
- 2368 $_pos = $_len;
- 2369 }
- 2370 return $_ciphertext;
- 2371 ';
- 2372
- 2373 $decrypt = $init_encrypt . '
- 2374 $_plaintext = "";
- 2375 $_buffer = &$self->debuffer;
- 2376
- 2377 if ($self->continuousBuffer) {
- 2378 $_iv = &$self->decryptIV;
- 2379 $_pos = &$_buffer["pos"];
- 2380 } else {
- 2381 $_iv = $self->decryptIV;
- 2382 $_pos = 0;
- 2383 }
- 2384 $_len = strlen($_text);
- 2385 $_i = 0;
- 2386 if ($_pos) {
- 2387 $_orig_pos = $_pos;
- 2388 $_max = '.$block_size.' - $_pos;
- 2389 if ($_len >= $_max) {
- 2390 $_i = $_max;
- 2391 $_len-= $_max;
- 2392 $_pos = 0;
- 2393 } else {
- 2394 $_i = $_len;
- 2395 $_pos+= $_len;
- 2396 $_len = 0;
- 2397 }
- 2398 $_plaintext = substr($_iv, $_orig_pos) ^ $_text;
- 2399 $_iv = substr_replace($_iv, substr($_text, 0, $_i), $_orig_pos, $_i);
- 2400 }
- 2401 while ($_len >= '.$block_size.') {
- 2402 $in = $_iv;
- 2403 '.$encrypt_block.'
- 2404 $_iv = $in;
- 2405 $cb = substr($_text, $_i, '.$block_size.');
- 2406 $_plaintext.= $_iv ^ $cb;
- 2407 $_iv = $cb;
- 2408 $_len-= '.$block_size.';
- 2409 $_i+= '.$block_size.';
- 2410 }
- 2411 if ($_len) {
- 2412 $in = $_iv;
- 2413 '.$encrypt_block.'
- 2414 $_iv = $in;
- 2415 $_plaintext.= $_iv ^ substr($_text, $_i);
- 2416 $_iv = substr_replace($_iv, substr($_text, $_i), 0, $_len);
- 2417 $_pos = $_len;
- 2418 }
- 2419
- 2420 return $_plaintext;
- 2421 ';
- 2422 break;
- 2423 case CRYPT_MODE_OFB:
- 2424 $encrypt = $init_encrypt . '
- 2425 $_ciphertext = "";
- 2426 $_plaintext_len = strlen($_text);
- 2427 $_xor = $self->encryptIV;
- 2428 $_buffer = &$self->enbuffer;
- 2429
- 2430 if (strlen($_buffer["xor"])) {
- 2431 for ($_i = 0; $_i < $_plaintext_len; $_i+= '.$block_size.') {
- 2432 $_block = substr($_text, $_i, '.$block_size.');
- 2433 if (strlen($_block) > strlen($_buffer["xor"])) {
- 2434 $in = $_xor;
- 2435 '.$encrypt_block.'
- 2436 $_xor = $in;
- 2437 $_buffer["xor"].= $_xor;
- 2438 }
- 2439 $_key = $self->_string_shift($_buffer["xor"], '.$block_size.');
- 2440 $_ciphertext.= $_block ^ $_key;
- 2441 }
- 2442 } else {
- 2443 for ($_i = 0; $_i < $_plaintext_len; $_i+= '.$block_size.') {
- 2444 $in = $_xor;
- 2445 '.$encrypt_block.'
- 2446 $_xor = $in;
- 2447 $_ciphertext.= substr($_text, $_i, '.$block_size.') ^ $_xor;
- 2448 }
- 2449 $_key = $_xor;
- 2450 }
- 2451 if ($self->continuousBuffer) {
- 2452 $self->encryptIV = $_xor;
- 2453 if ($_start = $_plaintext_len % '.$block_size.') {
- 2454 $_buffer["xor"] = substr($_key, $_start) . $_buffer["xor"];
- 2455 }
- 2456 }
- 2457 return $_ciphertext;
- 2458 ';
- 2459
- 2460 $decrypt = $init_encrypt . '
- 2461 $_plaintext = "";
- 2462 $_ciphertext_len = strlen($_text);
- 2463 $_xor = $self->decryptIV;
- 2464 $_buffer = &$self->debuffer;
- 2465
- 2466 if (strlen($_buffer["xor"])) {
- 2467 for ($_i = 0; $_i < $_ciphertext_len; $_i+= '.$block_size.') {
- 2468 $_block = substr($_text, $_i, '.$block_size.');
- 2469 if (strlen($_block) > strlen($_buffer["xor"])) {
- 2470 $in = $_xor;
- 2471 '.$encrypt_block.'
- 2472 $_xor = $in;
- 2473 $_buffer["xor"].= $_xor;
- 2474 }
- 2475 $_key = $self->_string_shift($_buffer["xor"], '.$block_size.');
- 2476 $_plaintext.= $_block ^ $_key;
- 2477 }
- 2478 } else {
- 2479 for ($_i = 0; $_i < $_ciphertext_len; $_i+= '.$block_size.') {
- 2480 $in = $_xor;
- 2481 '.$encrypt_block.'
- 2482 $_xor = $in;
- 2483 $_plaintext.= substr($_text, $_i, '.$block_size.') ^ $_xor;
- 2484 }
- 2485 $_key = $_xor;
- 2486 }
- 2487 if ($self->continuousBuffer) {
- 2488 $self->decryptIV = $_xor;
- 2489 if ($_start = $_ciphertext_len % '.$block_size.') {
- 2490 $_buffer["xor"] = substr($_key, $_start) . $_buffer["xor"];
- 2491 }
- 2492 }
- 2493 return $_plaintext;
- 2494 ';
- 2495 break;
- 2496 case CRYPT_MODE_STREAM:
- 2497 $encrypt = $init_encrypt . '
- 2498 $_ciphertext = "";
- 2499 '.$encrypt_block.'
- 2500 return $_ciphertext;
- 2501 ';
- 2502 $decrypt = $init_decrypt . '
- 2503 $_plaintext = "";
- 2504 '.$decrypt_block.'
- 2505 return $_plaintext;
- 2506 ';
- 2507 break;
- 2508
- 2509 default:
- 2510 $encrypt = $init_encrypt . '
- 2511 $_ciphertext = "";
- 2512 $_plaintext_len = strlen($_text);
- 2513
- 2514 $in = $self->encryptIV;
- 2515
- 2516 for ($_i = 0; $_i < $_plaintext_len; $_i+= '.$block_size.') {
- 2517 $in = substr($_text, $_i, '.$block_size.') ^ $in;
- 2518 '.$encrypt_block.'
- 2519 $_ciphertext.= $in;
- 2520 }
- 2521
- 2522 if ($self->continuousBuffer) {
- 2523 $self->encryptIV = $in;
- 2524 }
- 2525
- 2526 return $_ciphertext;
- 2527 ';
- 2528
- 2529 $decrypt = $init_decrypt . '
- 2530 $_plaintext = "";
- 2531 $_text = str_pad($_text, strlen($_text) + ('.$block_size.' - strlen($_text) % '.$block_size.') % '.$block_size.', chr(0));
- 2532 $_ciphertext_len = strlen($_text);
- 2533
- 2534 $_iv = $self->decryptIV;
- 2535
- 2536 for ($_i = 0; $_i < $_ciphertext_len; $_i+= '.$block_size.') {
- 2537 $in = $_block = substr($_text, $_i, '.$block_size.');
- 2538 '.$decrypt_block.'
- 2539 $_plaintext.= $in ^ $_iv;
- 2540 $_iv = $_block;
- 2541 }
- 2542
- 2543 if ($self->continuousBuffer) {
- 2544 $self->decryptIV = $_iv;
- 2545 }
- 2546
- 2547 return $self->_unpad($_plaintext);
- 2548 ';
- 2549 break;
- 2550 }
- 2551
- 2552
- 2553 if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
- 2554 eval('$func = function ($_action, &$self, $_text) { ' . $init_crypt . 'if ($_action == "encrypt") { ' . $encrypt . ' } else { ' . $decrypt . ' } };');
- 2555 return $func;
- 2556 }
- 2557
- 2558 return create_function('$_action, &$self, $_text', $init_crypt . 'if ($_action == "encrypt") { ' . $encrypt . ' } else { ' . $decrypt . ' }');
- 2559 }
- 2560
- 2561
- 2562
- 2563
- 2564
- 2565
- 2566
- 2567
- 2568
- 2569
- 2570
- 2571
- 2572
- 2573
- 2574
- 2575
- 2576 function &_getLambdaFunctions()
- 2577 {
- 2578 static $functions = array();
- 2579 return $functions;
- 2580 }
- 2581
- 2582
- 2583
- 2584
- 2585
- 2586
- 2587
- 2588
- 2589
- 2590 function _hashInlineCryptFunction($bytes)
- 2591 {
- 2592 if (!defined('CRYPT_BASE_WHIRLPOOL_AVAILABLE')) {
- 2593 define('CRYPT_BASE_WHIRLPOOL_AVAILABLE', (bool)(extension_loaded('hash') && in_array('whirlpool', hash_algos())));
- 2594 }
- 2595
- 2596 $result = '';
- 2597 $hash = $bytes;
- 2598
- 2599 switch (true) {
- 2600 case CRYPT_BASE_WHIRLPOOL_AVAILABLE:
- 2601 foreach (str_split($bytes, 64) as $t) {
- 2602 $hash = hash('whirlpool', $hash, true);
- 2603 $result .= $t ^ $hash;
- 2604 }
- 2605 return $result . hash('whirlpool', $hash, true);
- 2606 default:
- 2607 $len = strlen($bytes);
- 2608 for ($i = 0; $i < $len; $i+=20) {
- 2609 $t = substr($bytes, $i, 20);
- 2610 $hash = pack('H*', sha1($hash));
- 2611 $result .= $t ^ $hash;
- 2612 }
- 2613 return $result . pack('H*', sha1($hash));
- 2614 }
- 2615 }
- 2616
- 2617
- 2618
- 2619
- 2620
- 2621
- 2622
- 2623
- 2624
- 2625
- 2626 function safe_intval($x)
- 2627 {
- 2628 switch (true) {
- 2629 case is_int($x):
- 2630
- 2631 case version_compare(PHP_VERSION, '5.3.0') >= 0 && (php_uname('m') & "\xDF\xDF\xDF") != 'ARM':
- 2632
- 2633 case (PHP_OS & "\xDF\xDF\xDF") === 'WIN':
- 2634 return $x;
- 2635 }
- 2636 return (fmod($x, 0x80000000) & 0x7FFFFFFF) |
- 2637 ((fmod(floor($x / 0x80000000), 2) & 1) << 31);
- 2638 }
- 2639
- 2640
- 2641
- 2642
- 2643
- 2644
- 2645
- 2646 function safe_intval_inline()
- 2647 {
- 2648
- 2649 switch (true) {
- 2650 case defined('PHP_INT_SIZE') && PHP_INT_SIZE == 8:
- 2651 case version_compare(PHP_VERSION, '5.3.0') >= 0 && (php_uname('m') & "\xDF\xDF\xDF") != 'ARM':
- 2652 case (PHP_OS & "\xDF\xDF\xDF") === 'WIN':
- 2653 return '%s';
- 2654 break;
- 2655 default:
- 2656 $safeint = '(is_int($temp = %s) ? $temp : (fmod($temp, 0x80000000) & 0x7FFFFFFF) | ';
- 2657 return $safeint . '((fmod(floor($temp / 0x80000000), 2) & 1) << 31))';
- 2658 }
- 2659 }
- 2660 }